/*
 * @Author: 亚亚
 */
/*
 * @Author: 亚亚
 */

interface ComponentType <T>{
  [item: string]: T;
};

export const autoImportComponents = function() {
  //自动注入对应组件
  const components: ComponentType<string> = {};
  const files: ComponentType<any> = import.meta.glob('./components/**/*.vue', { eager: true });
  Object.keys(files).forEach((fileName) => {
    const name = fileName.replace(/\.\/components\/|\/index.vue/g, '');
    components[name] = files[fileName].default;
  });
  return components
}

export const autoImportProps = function(props) {
  //自动引入对应组件Props
  const componentProps: ComponentType<any> = {}
  const propFiles: ComponentType<any> = import.meta.glob('./components/**/*.ts', { eager: true });
  Object.keys(propFiles).forEach((fileName) => {
      const name = fileName.replace(/\.\/components\/|\/index.ts/g, '');
      componentProps[name] = propFiles[fileName].props;
  });
  const lodash = getCurrentInstance()?.appContext.config.globalProperties._
  //组件默认参数合并
  props.searchColumns.map((e: any, index: number) => {
    const val: ComponentType<any> = {}
    for(const item in componentProps[e.element]) {
      const prop = componentProps[e.element][item]?.default
      if(typeof prop == 'function') {
        val[item] = prop()
      }else {
        val[item] = prop
      }
    }
    props.searchColumns[index] = lodash.merge(val, e)
  })
}

